package nju.bean.meeting.meetingclient;


import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

import junit.framework.Assert;

import nju.bean.meeting.clientManagement.MeetingClient;
import nju.bean.meeting.clientManagement.UmlClient;
import nju.bean.meeting.guiManagement.GUIManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MeetingClientTest {

	private MeetingClient mc; 
	private HashMap<String,Object> startInfor = new HashMap<String,Object>();
	private HashMap<String,Object> addInfor = new HashMap<String,Object>();
	static final private int START = 0;
	static final private int ADD = 1;
	
	String mId = "123123";
	String mName = "first";
	int portList[] = {101,120,130};
	String userList[] = {"wy","gsw","cjy"}; 
	Color color = Color.black;
	int dex = 4;
	Color colorList[] = {Color.black,Color.blue,Color.red};
	int numOfUser = 6;
	int diagramIndex = 2;
	
	@Before
	public void setUp() throws Exception {
		mc = MeetingClient.getInstance();
		this.initializeInfor();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSignPort(){
		String port = (String)startInfor.get("portList");
		try {
			Method testMethod = mc.getClass().getDeclaredMethod("neatenPort", String.class);
			testMethod.setAccessible(true);
			testMethod.invoke(mc, port);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String,Integer> result = mc.getPort();
		int a1 = result.get("mainPort");
		int b1 = result.get("drawCommandReceiver");
		int c1 = result.get("drawCommandSender");
		int len = result.size();
		Assert.assertEquals(a1,portList[0]);
		Assert.assertEquals(b1,portList[1]);
		Assert.assertEquals(c1,portList[2]);
		Assert.assertEquals(portList.length, len);
	}
	
	@Test
	public void testNeatenUser(){
		try {
			Method testMethod = mc.getClass().getDeclaredMethod("neatenUser", HashMap.class,int.class);
			testMethod.setAccessible(true);
			testMethod.invoke(mc,startInfor,START);
			boolean result1 = UmlClient.exist();
			boolean result2 = GUIManager.exist(); 
			Assert.assertEquals(result1, true);
			Assert.assertEquals(result2, true);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void initializeInfor(){
		String ports = "";
		int n = 0;
		for(;n<portList.length-1;n++){
			ports += portList[n]+"/";
		}
		ports+=portList[n];
		ArrayList<Color> colors = new ArrayList<Color>();
		for(int i = 0;i<colorList.length;i++){
			colors.add(colorList[i]);
		}
		ArrayList<String> users = new ArrayList<String>();
		for(int i = 0;i<userList.length;i++){
			users.add(userList[i]);
		}
		startInfor.put("mId", mId);
		startInfor.put("portList", ports);
		startInfor.put("userColor",color);
		addInfor.put("mName",mName);
		addInfor.put("portList", ports);
		addInfor.put("userList", userList);
		addInfor.put("userColor",color);
		addInfor.put("dex", dex);
		addInfor.put("colorList",colors);
		addInfor.put("numOfUser", numOfUser);
		addInfor.put("diagramIndex", diagramIndex);
	}
}
